<?php
	
		class Hits extends CI_Controller {
				
				function __construct(){
						parent::__construct();
				}
					
				function track() {
						$uri  = $this->uri->uri_to_assoc(2);
						$type = @$uri['t'];
						$id   = @$uri['id'];
						$ref  = @$uri['ref'];
						
						if($type=='' OR $id =='')
						{
								show_error('Incorrect URL');
						}
						else
						{
								$type = $type=='t'?'text':($type=='b'?'banner':'');
								
								if($type=='banner')
								{
										$banner = new Banner();
										$banner->where('id', (int)$id)
											   ->where('id >', 0)
											   ->get(1);
										if($banner->exists() && $banner->dest)
										{
												$this->_resolve_referrer($ref);
												
												$dest = $banner->dest;
												$hit = new Hit();
												$hit->where('h_added >', time()-24*60*60)
													->where('type', $type)
													->where('ip', $this->input->ip_address())
													->get(1);
												if(!$hit->exists())
												{
														$banner->clear();
														$banner->where('id', (int)$id)
															   ->where('id >', 0)
															   ->update(array('clicks'=>'clicks+1'), FALSE);
												}
												
												$this->load->library('user_agent');
												
												$hit->clear();
												$hit->item_id 	= $id;
												$hit->type  	= $type;
												$hit->ip  		= $this->input->ip_address();
												$hit->browser 	= $this->agent->browser();
												$hit->mobile  	= $this->agent->mobile();
												$hit->robot  	= $this->agent->robot();
												$hit->platform  = $this->agent->platform();
												$hit->referrer  = $this->agent->referrer();
												$hit->agent   	= $this->agent->agent_string();
												$hit->h_added  	= time();
												$hit->save();												
												
												redirect($dest);
										}
										else
										{
												show_error('Incorrect URL');
										}
								}
								elseif($type=='text')
								{
										$link = new Text_link();
										$link->where('id', (int)$id)
											 ->where('id >', 0)
											 ->get(1);
										if($link->exists() && $link->dest)
										{
												$this->_resolve_referrer($ref);
												
												$dest = $link->dest;
												$hit = new Hit();
												$hit->where('h_added >', time()-24*60*60)
													->where('type', 'text_link')
													->where('ip', $this->input->ip_address())
													->get(1);
												if(!$hit->exists())
												{
														$link->clear();
														$link->where('id', (int)$id)
															   ->where('id >', 0)
															   ->update(array('clicks'=>'clicks+1'), FALSE);
												}
												
												$this->load->library('user_agent');
												
												$hit->clear();
												$hit->item_id 	= $id;
												$hit->type  	= 'text_link';
												$hit->ip  		= $this->input->ip_address();
												$hit->browser 	= $this->agent->browser();
												$hit->mobile  	= $this->agent->mobile();
												$hit->robot  	= $this->agent->robot();
												$hit->platform  = $this->agent->platform();
												$hit->referrer  = $this->agent->referrer();
												$hit->agent   	= $this->agent->agent_string();
												$hit->h_added  	= time();
												$hit->save();
												
												redirect($dest);
										}
										else
										{
												show_error('Incorrect URL');
										}
								}
						}
				
						die();
				}
		
				function _resolve_referrer($ref=NULL) {
						$this->load->helper('cookie');
						if(!$ref)
						{
								for($i=3;$i<$this->uri->total_segments();$i++)
								{
										if( $this->uri->segment($i)=='ref' && $this->uri->segment($i+1) )
										{
												$ref = (int)$this->uri->segment($i);
												break;
										}
								}
						}
						if($ref>0)
						{
								$referer = new User();
								$referer->where('id',(int)$ref)->get(1);
								if( $referer->exists() )
								{
										$this->load->library('encrypt');
										$opts = read_options('aff_ttl', 'OBJECT');
										$cookie = array(
											'name'   => '__rid',
											'value'  => $this->encrypt->encode($ref),
											'expire' => $opts->aff_ttl ? $opts->aff_ttl :2678400,
											'domain' => '.' . parse_url(base_url(), PHP_URL_HOST),
											'path'   => '/'
										);
										$this->input->set_cookie($cookie); 
										return $ref;
								}
						}
						elseif($this->input->cookie('__rid'))
						{
								$ref = (int)$this->encrypt->decode($this->input->cookie('__rid'));
						}
						return $ref;
				}
		}
		
?>